/*
 *  Copyright 2007-2008 by Matthias Buch-Kromann <mbk.isv@cbs.dk>.
 *  *
 *  This file is part of the Open-source Dependency Toolkit (OSDT),
 *  see http://code.google.com/p/open-source-dependency-toolkit.
 *  
 *  The OSDT is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as 
 *  published by the Free Software Foundation, either version 3 of 
 *  the License, or (at your option) any later version.
 * 
 *  The OSDT is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public 
 *  License along with the OSDT in the files COPYING and COPYING.LESSER.
 *  If not, see http://www.gnu.org/licenses.
 */

package org.osdtsystem.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

/**
 *
 * @author Matthias Buch-Kromann <mbk.isv@cbs.dk>
 */
public class MemoryStorage implements Storage {
    Map<String,Object> namedStorage = new HashMap<String,Object>();
    
    @Override
    public <X> List<X> newList(Object... ids) {
        // Try to retrieve list from named storage
        String name = name(ids);
        List<X> list = null;
        if (name != null)
            list = (List<X>) namedStorage.get(name);
        
        // Create list if non-existent
        if (list == null)
            list = new ArrayList<X>();
        
        // Store list under name
        if (name != null)
            namedStorage.put(name, list);
        
        // Return list
        return list;
    }

    @Override
    public <X> Set<X> newSet(Object... ids) {
        // Try to retrieve set from named storage
        String name = name(ids);
        Set<X> set = null;
        if (name != null)
            set = (Set<X>) namedStorage.get(name);
        
        // Create set if non-existent
        if (set == null)
            set = new HashSet<X>();
        
        // Store set under name
        if (name != null)
            namedStorage.put(name, set);
        
        // Return set
        return set;
    }
    
    public <X> SortedSet<X> newSortedSet(Object... ids) {
        // Try to retrieve set from named storage
        String name = name(ids);
        SortedSet<X> set = null;
        if (name != null)
            set = (SortedSet<X>) namedStorage.get(name);
        
        // Create set if non-existent
        if (set == null)
            set = new TreeSet<X>();
        
        // Store set under name
        if (name != null)
            namedStorage.put(name, set);
        
        // Return set
        return set;
    }

    @Override
    public <X> LinkedHashSet<X> newLinkedHashSet(Object... ids) {
        String name = name(ids);
        LinkedHashSet<X> set = null;
        if (name != null)
            set = (LinkedHashSet<X>) namedStorage.get(name);
        
        // Create map if non-existent
        if (set == null)
            set = new LinkedHashSet<X>();
        
        // Store map under name
        if (name != null)
            namedStorage.put(name, set);
        
        // Return map
        return set;
    }

    
    @Override
    public <X, Y> Map<X, Y> newMap(Object... ids) {
        String name = name(ids);
        Map<X,Y> map = null;
        if (name != null)
            map = (Map<X,Y>) namedStorage.get(name);
        
        // Create map if non-existent
        if (map == null)
            map = new HashMap<X,Y>();
        
        // Store map under name
        if (name != null)
            namedStorage.put(name, map);
        
        // Return map
        return map;
    }

    @Override
    public <X, Y> LinkedHashMap<X, Y> newLinkedHashMap(Object... ids) {
        String name = name(ids);
        LinkedHashMap<X,Y> map = null;
        if (name != null)
            map = (LinkedHashMap<X,Y>) namedStorage.get(name);
        
        // Create map if non-existent
        if (map == null)
            map = new LinkedHashMap<X,Y>();
        
        // Store map under name
        if (name != null)
            namedStorage.put(name, map);
        
        // Return map
        return map;
    }

    @Override
    public String name(Object... ids) {
        return null;
    }

    @Override
    public <X> List<X> smallList(Object... ids) {
        return this.<X>newList(ids);
    }

    @Override
    public <X> Set<X> smallSet(Object... ids) {
        return this.<X>newSet(ids);
    }

    @Override
    public <X, Y> Map<X, Y> smallMap(Object... ids) {
        return this.<X,Y>newMap(ids);
    }

    @Override
    public <X> List<X> bigList(Object... ids) {
        return this.<X>newList(ids);
    }

    @Override
    public <X> Set<X> bigSet(Object... ids) {
        return this.<X>newSet(ids);
    }

    @Override
    public <X, Y> Map<X, Y> bigMap(Object... ids) {
        return this.<X,Y>newMap(ids);
    }

    @Override
    public <X> SortedSet<X> bigSortedSet(Object... ids) {
        return this.<X>newSortedSet(ids);
    }

    @Override
    public <X> SortedSet<X> smallSortedSet(Object... ids) {
        return this.<X>newSortedSet(ids);
    }

    @Override
    public <X> LinkedHashSet<X> smallLinkedHashSet(Object... ids) {
        return this.<X>newLinkedHashSet(ids);
    }

    @Override
    public <X,Y> LinkedHashMap<X,Y> smallLinkedHashMap(Object... ids) {
        return this.<X,Y>newLinkedHashMap(ids);
    }

}
    
